package wiki.cn.edu.tju;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class JsonGen {
	public static List<Map<String, Object>> pageNodes = new ArrayList<Map<String, Object>>();
	public static List<Map<String, Object>> pageLinks = new ArrayList<Map<String, Object>>();
	public static final String[] catKeyset = {"cl_from", "cl_type"};
	public static final String[] pageKeyset = {"page_title"};
	public static long cl_from;
	public static String cl_type, pgQueryStr, curPageTitle;
	public static long pageNum=0, relationNum=0, errorNum=0;
	
	public static void main(String[] args){
//		int rootNodeId = 1197919;//中華文化
		String rootPageTitle = "中国博物馆";
		String clQueryStr = "SELECT cl_from,cl_type FROM my_wikidb.categorylinks where cl_to='"+ rootPageTitle +"';";
		Map<String, Object> nodeItem = new HashMap<String, Object>();
		nodeItem.put("name", rootPageTitle);
		nodeItem.put("group", 1); //////fixed for now
		pageNodes.add(nodeItem);
		loadData(nodeItem, clQueryStr);
		System.out.println("pageNum="+pageNum+", relationNum="+relationNum+", errorNum="+errorNum);
		genJson();
	}

	public static void genJson(){
		String nodestr = "\"nodes\":[";
		String linkstr = "\n\"links\":[";
		for (Map<String, Object> map : pageNodes) {
			nodestr += "\n{\"name\":\""+map.get("name")+"\",\"group\":"+map.get("group")+"},";
		}
		nodestr = nodestr.substring(0, nodestr.length()-1);
		nodestr += "\n],";
		
		for (Map<String, Object> map : pageLinks) {
			linkstr += "\n{\"source\":"+map.get("source")+",\"target\":"+map.get("target")+",\"value\":"+map.get("value")+"},";
		}
		linkstr = linkstr.substring(0, linkstr.length()-1);
		linkstr += "\n]";
		String jsonStr = "{\n" + nodestr + linkstr + "\n}";
		System.out.println(jsonStr);
	}
	
	public static void loadData(Map<String, Object> parentNode, String sqlStr) {
		List<Map<String, Object>> resultMap = null;
		try {
			resultMap = DataConTool.getAttribute(sqlStr, catKeyset);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return;
		}
		int parentIndex = pageNodes.lastIndexOf(parentNode);
		System.out.println("+++++++++++++++++++"+parentNode.get("name"));
		for (Map<String, Object> map : resultMap) {
			cl_from = Long.parseLong((String) map.get("cl_from"));
			cl_type = (String)map.get("cl_type");
			pgQueryStr = "SELECT page_title FROM my_wikidb.my_wikidbpage where page_id="
					+ cl_from + ";";
			Map<String, Object> nodeItem = new HashMap<String, Object>();
			try {
				curPageTitle = DataConTool.getAttribute(pgQueryStr, pageKeyset)
						.get(0).get("page_title").toString();
				nodeItem.put("name", curPageTitle);
				nodeItem.put("group", 1); // ////fixed for now
				if(!pageNodes.contains(nodeItem)){
					pageNodes.add(nodeItem);
//					relationNum++;
//					Map<String, Object> linkItem = new HashMap<String, Object>();
//					linkItem.put("source", pageNodes.lastIndexOf(nodeItem));
//					linkItem.put("target", parentIndex);
//					linkItem.put("value", 1); // ////fixed for now
//					pageLinks.add(linkItem);
				}
				relationNum++;
				Map<String, Object> linkItem = new HashMap<String, Object>();
				linkItem.put("source", pageNodes.lastIndexOf(nodeItem));
				linkItem.put("target", parentIndex);
				linkItem.put("value", 1); // ////fixed for now
				pageLinks.add(linkItem);
//				else{
//					System.out.println("@@@@@@@@@@@@@@@@@@@@@@@   "+curPageTitle);
//					continue;
//				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				errorNum++;
				continue;
			}
			if ("page".equals(cl_type)) {
				System.out.println(curPageTitle);
				pageNum++;
			} else if ("subcat".equals(cl_type)) {
				String ctQueryStr = "SELECT cl_from, cl_type FROM my_wikidb.categorylinks where cl_to='"
						+ curPageTitle + "';";
				loadData(nodeItem, ctQueryStr);
			}
		}
	}
}
